package aod.android.com.bernay.loginandregister;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.http.HttpUrl;
import aod.android.com.bernay.loginandregister.bean.UserDataBean;
import aod.android.com.bernay.util.InputFormatUtil;
import aod.android.com.bernay.util.SharePreUtils;
import aod.android.com.bernay.view.activity.MainActivity;
import chen.lion.hilib.http.HiCallback;
import chen.lion.hilib.http.Http;
import chen.lion.hilib.http.RealResponse;
import chen.lion.hilib.util.GeneralUtils;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * <p>文件描述：登陆页面 <p>
 * <p>作者：${SYC}<p>
 * <p>创建时间：2019/4/1<p>
 */
public class LoginActivity extends BernayBaseActivity implements View.OnClickListener {

    @HiView(R.id.title_iv_back)
    ImageView title_iv_back;
    @HiView(R.id.login_tv)
    TextView login_tv;
    @HiView(R.id.login_register_tv)
    TextView login_register_tv;
    @HiView(R.id.login_forgetpwd_tv)
    TextView login_forgetpwd_tv;
    @HiView(R.id.title_tv_name)
    TextView title_tv_name;
    @HiView(R.id.login_phone_ed)
    EditText login_phone_ed;
    @HiView(R.id.login_pwd_ed)
    EditText login_pwd_ed;
    //手机号
    private String mobiles = "";
    //密码
    private String loginPwd = "";
    private InputFormatUtil inputFormatUtil = new InputFormatUtil();

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        title_tv_name.setText(getString(R.string.login_title));
        title_iv_back.setOnClickListener(this);
        login_tv.setOnClickListener(this);
        login_register_tv.setOnClickListener(this);
        login_forgetpwd_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.title_iv_back:
                finish();
                break;
            case R.id.login_tv:
                mobiles = login_phone_ed.getText().toString();
                loginPwd = login_pwd_ed.getText().toString();
                if (TextUtils.isEmpty(mobiles)) {
                    showError("请输入手机号为空");
                    return;
                }
                if (!GeneralUtils.isMobileNO(mobiles)) {
                    showError("输入手机号格式错误");
                    return;
                }
                if (TextUtils.isEmpty(loginPwd)) {
                    showError("输入密码为空");
                    return;
                }
                if (!GeneralUtils.isPwdNO(loginPwd)) {
                    showError("输入密码格式错误");
                    return;
                }
                goLogin();
                break;
            case R.id.login_register_tv:
                bundle.putString("register", "1");//1为注册 2为忘记密码
                goActivity(RegisterAndForgetPwdActivity.class, bundle);
                break;
            case R.id.login_forgetpwd_tv:
                bundle.putString("register", "2");//1为注册 2为忘记密码
                goActivity(RegisterAndForgetPwdActivity.class, bundle);
                break;
        }
    }

    private void goLogin() {
        BernayApi.http().login(mobiles, loginPwd, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                UserDataBean userDataBean = gson.fromJson(json, UserDataBean.class);
                if (userDataBean.getStatus() == 1) {
                    SharePreUtils.newInstance(LoginActivity.this).updata(userDataBean.getData().get(0).getUserId());
                    JPushInterface.setAlias(mContext, userDataBean.getData().get(0).getUserId(), new TagAliasCallback() {
                        @Override
                        public void gotResult(int i, String s, Set<String> set) {
                            LogUtils.e("gotResult i="+i+",s="+s+",set="+new Gson().toJson(set));
                        }
                    });
                    SharePreUtils.newInstance(LoginActivity.this).updataToken(userDataBean.getData().get(0).getToken());
                    goActivity(MainActivity.class);
                    finish();
                } else if (userDataBean.getStatus() == -1) {
                    showToast("登录失败");
                }
            }

            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });


    }
}
